package com.fsnbigdata.service.impl;

import com.fsnbigdata.domain.ZhEcologyNav;
import com.fsnbigdata.domain.ZhKopp;
import com.fsnbigdata.repository.ZhEcologyNavRepository;
import com.fsnbigdata.service.api.ZhEcologyNavService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 旅游导航
 */
@Transactional
@Service
public class ZhEcologyNavServiceImpl implements ZhEcologyNavService {

    @Autowired
    private ZhEcologyNavRepository zhEcologyNavRepository;

    /**
     * 查询所有
     * @return
     */
    @Override
    public List<ZhEcologyNav> findAll() {
        return zhEcologyNavRepository.findAll( );
    }

    /**
     * 根据id查询单个
     * @param id
     * @return
     */
    @Override
    public ZhEcologyNav findById(Long id) {
        return zhEcologyNavRepository.findOne( id );
    }

    /**
     * 修改数据
     * @param zhEcologyNav
     * @return
     */
    @Override
    public ZhEcologyNav update(ZhEcologyNav zhEcologyNav) {
        return zhEcologyNavRepository.save( zhEcologyNav );
    }

    /**
     * 删除一个
     * @param id
     * @return
     */
    @Override
    public Integer deleteOne(Long id) {
        Integer i = 1;
        try {
            zhEcologyNavRepository.delete( id );
        }catch (Exception e){
            i=0;
            System.out.println(e.getMessage());
        }
        return i;
    }

    /**
     * 删除多个
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public Integer deleteMany(String ids) {
        String[] strings = ids.split( "," );
        List<ZhEcologyNav> zhEcologyNavs = new ArrayList <>(  );
        for (Integer i = 0;i<strings.length;i++){
            ZhEcologyNav zhEcologyNav = new ZhEcologyNav();
            zhEcologyNav.setId( Long.parseLong( strings[i] ) );
            zhEcologyNavs.add( zhEcologyNav );
        }
    try {
        zhEcologyNavRepository.deleteInBatch(zhEcologyNavs);
    }catch (Exception e){
        System.out.println(e.getMessage());
        return 0;
    }
        return 1;
    }

    /**
     * 分页查询
     * @param pageable
     */
    @Override
    public Page<ZhEcologyNav> pageList(String city, String type, String condition,String creator, Pageable pageable) {

        Specification<ZhEcologyNav> specification=new Specification<ZhEcologyNav>() {
            @Override
            public Predicate toPredicate(Root<ZhEcologyNav> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Path<String> _name=root.get("name");
                Path<String> name_en = root.get("name_en");
                Path<String> s_type = root.get("category");
                Path<String> s_city = root.get("city");
                Path<String> _creator = root.get("creator");
                Predicate _key = criteriaBuilder.like(_name,"%"+condition+"%");
                Predicate _key1 = criteriaBuilder.like(name_en,"%"+condition+"%");
                List<Predicate> list = new ArrayList <Predicate>(  );
                if(!(condition==null||"".equals( condition ))){
                    list.add( criteriaBuilder.or( _key,_key1 ) );
                }
                if(!(city==null||"".equals( city ))){
                    list.add( criteriaBuilder.equal( s_city,city ) );
                }
                if(!(type==null||"".equals( type ))){
                    list.add( criteriaBuilder.equal( s_type,type ) );
                }
                if(!(creator==null||"".equals( creator ))){
                    list.add( criteriaBuilder.equal( _creator,creator ) );
                }
                Predicate[] p = new Predicate[list.size()];
                return criteriaBuilder.and( list.toArray(p) );

            }
        };
        Page<ZhEcologyNav> zhEcologyNavs = zhEcologyNavRepository.findAll(specification,pageable);
        return zhEcologyNavs;
    }

    @Override
    public List <String> findAllCity() {
        return zhEcologyNavRepository.findAllCity();
    }

    @Override
    public Long[] countType(String city) {
        List<Object> objects = new ArrayList <>(  );
        Long[] type = new Long[6];
        if(city==null||"".equals( city )){
           objects = zhEcologyNavRepository.countType();
        }else {
            objects = zhEcologyNavRepository.countTypeByCity( city );
        }
        for(Object o:objects){
            Object[] objects1 = (Object[]) o;
            switch (String.valueOf( objects1[0] )){
                case "自然保护区":
                    type[0] = (Long) objects1[1];
                    break;
                case "森林公园":
                    type[1] = (Long) objects1[1];
                    break;
                case "湿地公园":
                    type[2] = (Long) objects1[1];
                    break;
                case "风景名胜区":
                    type[3] = (Long) objects1[1];
                    break;
                case "保育基地":
                    type[4] = (Long) objects1[1];
                    break;
                case  "水产种质资源保护区":
                    type[5] = (Long) objects1[1];
                    break;
                    default:
                        break;
            }

        }
        for(int i = 0; i < type.length; i++){
            if(type[i] == null){
                type[i] = 0L;
            }
        }
        return type;
    }

    @Override
    public List <ZhEcologyNav> queryByAlphabet(String s) {
        return zhEcologyNavRepository.queryByAlphabet(s);
    }

    /**
     * 新增
     * @param zhEcologyNav
     * @return
     */
    @Transactional
    @Override
    public ZhEcologyNav save(ZhEcologyNav zhEcologyNav) {
        return zhEcologyNavRepository.save( zhEcologyNav );
    }
}
